package com.xudow.app.model;

import android.util.Xml;

import com.xudow.app.util.StringUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

public class Update implements Serializable {
    private static final long serialVersionUID = -1356876892850891288L;
    public final static String UTF8 = "UTF-8";

    private int versionCode;
    private String versionName;
    private String downloadUrl;
    private String updateLog;
    private HashMap<String,String> channels;

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public String getUpdateLog() {
        return updateLog;
    }

    public void setUpdateLog(String updateLog) {
        this.updateLog = updateLog;
    }

    public HashMap<String, String> getChannels() {
        return channels;
    }

    public void setChannels(HashMap<String, String> channels) {
        this.channels = channels;
    }

    public static Update parse(InputStream inputStream) throws IOException,
            Exception {
        Update update = null;
        HashMap<String,String> channels=null;
        // 获得XmlPullParser解析器
        XmlPullParser xmlParser = Xml.newPullParser();
        try {
            xmlParser.setInput(inputStream, UTF8);
            // 获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
            int evtType = xmlParser.getEventType();
            // 一直循环，直到文档结束
            while (evtType != XmlPullParser.END_DOCUMENT) {
                String tag = xmlParser.getName();
                switch (evtType) {
                    case XmlPullParser.START_TAG:
                        // 通知信息
                        if (tag.equalsIgnoreCase("android")) {
                            update = new Update();
                        } else if (update != null) {
                            if (tag.equalsIgnoreCase("versionCode")) {
                                update.setVersionCode(StringUtils.toInt(
                                        xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase("versionName")) {
                                update.setVersionName(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase("downloadUrl")) {
                                update.setDownloadUrl(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase("updateLog")) {
                                update.setUpdateLog(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase("channels")){
                                channels=new HashMap<String, String>();
                            }else if (null!=channels){
                                if (tag.equalsIgnoreCase("channel")){
                                    channels.put(xmlParser.getAttributeValue(0),xmlParser.nextText());
                                }
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (tag.equalsIgnoreCase("channels")){
                            update.setChannels(channels);
                        }
                        break;
                }
                // 如果xml没有结束，则导航到下一个节点
                evtType = xmlParser.next();
            }
        } catch (XmlPullParserException e) {

        } finally {
            inputStream.close();
        }
        return update;
    }
}

